#include "stdafx.h"
#include "Object.h"

cObject::cObject()
{
	m_Model = NULL;
	m_Texture = NULL;
	m_maLocalW.SetIdentity();
	m_fScaleX = 1.0f;
	m_fScaleY = 1.0f;
	m_fScaleZ = 1.0f;
	m_fRotX = 0.0f;
	m_fRotY = 0.0f;
	m_fRotZ = 0.0f;

	m_bIsChanged = false;
}

void cObject::Init(GLenum startTextureModel, char **fileTexture, int texCount, char *fileModelSource)
{
	m_Texture = new cObjectTexture();
	switch(startTextureModel)
	{
	case GL_TEXTURE_2D:
		{
			m_Texture->InitTexture2D(fileTexture, texCount);
		}
		break;
	case GL_TEXTURE_CUBE_MAP:
		{
			m_Texture->InitTextureCube(fileTexture);
		}
		break;
	}

	m_Model = new cObjectModel();
	m_Model->Init(fileModelSource);
}

void cObject::Scale(GLfloat x, GLfloat y, GLfloat z)
{
	m_fScaleX = x;
	m_fScaleY = y;
	m_fScaleZ = z;
	m_bIsChanged = true;
}

void cObject::RotateX(GLfloat angle)
{
	m_fRotX += angle;
	m_bIsChanged = true;
}

void cObject::RotateY(GLfloat angle)
{
	m_fRotY += angle;
	m_bIsChanged = true;
}

void cObject::RotateZ(GLfloat angle)
{
	m_fRotZ += angle;
	m_bIsChanged = true;
}

void cObject::Translate(GLfloat x, GLfloat y, GLfloat z)
{
	m_vPosition = Vector3(x, y, z);
	m_bIsChanged = true;
}

//Way for move on terrain
// Step 1: Choose constant value is distance form object to terrain;
// Step 2: Get Terrain Height Map at object position;
// Step 3: Get sum constant value and height map value
void cObject::MoveLeft(GLfloat delta)
{
}

void cObject::MoveRight(GLfloat delta)
{
}

void cObject::MoveBackword(GLfloat delta)
{
}

void cObject::MoveForword(GLfloat delta)
{
}

GLfloat cObject::UpdateHeightTerrian()
{
	return 0.0f;
}

void cObject::Update(ESContext *esContext, GLfloat deltaTime)
{
	if(m_bIsChanged)
	{
		Matrix maScale, maRot, maTran;
		Matrix maRotX, maRotY, maRotZ;

		maScale.SetScale(m_fScaleX, m_fScaleY, m_fScaleZ);
		maRotX.SetRotationX(m_fRotX);
		maRotY.SetRotationY(m_fRotY);
		maRotZ.SetRotationZ(m_fRotZ);
		maRot = maRotZ * maRotX * maRotY;

		maTran.SetTranslation(m_vPosition);

		m_maLocalW = maScale * maRot * maTran;
	}
}

GLuint *cObject::GetTextureID()
{
	return m_Texture->GetTextureID();
}

void cObject::Draw(ESContext *esContext)
{
}

cObject::~cObject()
{
	//SAFE_DEL(m_Texture);
	SAFE_DEL(m_Model);
}